package com.beordie.dao.impl;

import com.beordie.dao.ICarDao;
import com.beordie.model.Car;
import com.beordie.utils.DBUtils;

import javax.transaction.xa.XAResource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname CarDaoImpl
 * @Description car 表的数据操作
 * @Date 2021/7/11 21:13
 * @Created 30500
 */
public class CarDaoImpl extends DBUtils implements ICarDao {
    @Override
    public List<Car> getAllCars() {
        List<Car> cars = new ArrayList<>();

        resultSet = query(SELECT_ALL, null);
        try {
            while (resultSet.next()) {
                Car car = new Car();
                car.setId(resultSet.getInt("id"));
                car.setNumber(resultSet.getString("number"));
                car.setModel(resultSet.getString("model"));
                car.setFactory(resultSet.getString("factory"));
                car.setCreateTime(resultSet.getDate("createtime"));
                car.setSeat(resultSet.getInt("seat"));
                car.setColor(resultSet.getString("color"));
                car.setPrice(resultSet.getInt("price"));
                cars.add(car);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeAll();
        }
        return cars;
    }

    @Override
    public List<Car> getCarsByLimit(boolean isLimit, int start, int size) {
        List<Car> cars = new ArrayList<>();
        if (!isLimit) {
            return getAllCars();
        }
        List<Integer> params = new ArrayList<>();
        params.add(start);
        params.add(size);

        resultSet = query(SELECT_BY_LIMIT, params);
        try {
            while (resultSet.next()) {
                Car car = new Car();
                car.setId(resultSet.getInt("id"));
                car.setNumber(resultSet.getString("number"));
                car.setModel(resultSet.getString("model"));
                car.setFactory(resultSet.getString("factory"));
                car.setCreateTime(resultSet.getDate("createtime"));
                car.setSeat(resultSet.getInt("seat"));
                car.setColor(resultSet.getString("color"));
                car.setPrice(resultSet.getInt("price"));
                cars.add(car);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeAll();
        }
        return cars;
    }

    @Override
    public Car getCarByNumber(String number) {
        Car car = null;
        List<String> params = new ArrayList<>();
        params.add(number);

        resultSet = query(SELECT_CAR_BY_NUMBER, params);
        try {
            if (resultSet.next()) {
                car = new Car();
                car.setId(resultSet.getInt("id"));
                car.setNumber(resultSet.getString("number"));
                car.setModel(resultSet.getString("model"));
                car.setFactory(resultSet.getString("factory"));
                car.setCreateTime(resultSet.getDate("createtime"));
                car.setSeat(resultSet.getInt("seat"));
                car.setColor(resultSet.getString("color"));
                car.setPrice(resultSet.getInt("price"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeAll();
        }
        return car;
    }

    @Override
    public int insert(Car car) {
        List<Object> params = new ArrayList<>();
        params.add(car.getNumber());
        params.add(car.getModel());
        params.add(car.getFactory());
        params.add(car.getCreateTime());
        params.add(car.getSeat());
        params.add(car.getColor());
        params.add(car.getPrice());

        int result = 0;
        try {
            result= super.update(INSERT, params);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public int updateById(Car car) {
        List<Object> params = new ArrayList<>();
        params.add(car.getNumber());
        params.add(car.getModel());
        params.add(car.getFactory());
        params.add(car.getCreateTime());
        params.add(car.getSeat());
        params.add(car.getColor());
        params.add(car.getPrice());
        params.add(car.getId());

        int result = 0;
        try {
            result = super.update(UPDATE, params);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }

        return result;
    }

    @Override
    public int deleteById(int id) {
        List<Integer> params = new ArrayList<>();
        params.add(id);

        int result = 0;
        try {
            result = super.update(DELETE, params);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public int getCount() {
        resultSet = query(SELECT_COUNT_ALL, null);
        int result = -1;
        try {
            while (resultSet.next()) {
                result = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeAll();
        }
        return result;
    }
}
